package com.itheima.controller;


import com.itheima.manager.UserManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.aggregation.ArrayOperators;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {


    @Autowired
    UserManager userManager;


    // 用户列表
    @GetMapping("/manage/users")
    public ResponseEntity findUserInfoByPage(
            @RequestParam(value = "page", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {
        return userManager.findByPage(pageNum, pageSize);
    }


    // 用户详情
    @GetMapping("/manage/users/{userId}")
    public ResponseEntity findById(@PathVariable Long userId) {
        return userManager.findUserInfoById(userId);
    }


    //动态列表
    @GetMapping("/manage/messages")
    public ResponseEntity findMovementByConditionAndPage(
            @RequestParam(value = "page",defaultValue = "1")Integer pageNum,
            @RequestParam(value = "pagesize",defaultValue = "10")Integer pageSize,
            @RequestParam(value = "uid",required = false)Long userId,
            @RequestParam(required = false)String state){

         /*
        state 有可能是"",null,"''",0,1,2
         */
        Integer status = null;

        if (state == null || "".equals(state) || "''".equals(state) ) {
            status = null;
        }else{
            status = Integer.parseInt(state);
        }

        return userManager.findMovementByConditionAndPage(userId,status,pageNum,pageSize);
    }


    // 单条动态详情
    @GetMapping("/manage/messages/{movementId}")
    public ResponseEntity findMovementById(@PathVariable String movementId){
        return userManager.findMovementById(movementId);
    }


    //动态评论
    @GetMapping("/manage/messages/comments")
    public ResponseEntity findMovementCommentByPage(
            @RequestParam(value = "page",defaultValue = "1")Integer pageNum,
            @RequestParam(value = "pagesize",defaultValue = "10")Integer pageSize,
            @RequestParam("messageID") String movementId
    ){
        return userManager.findMovementCommentByPage(movementId,pageNum,pageSize);
    }


    @GetMapping("/manage/videos")
    public ResponseEntity findVideoByPage(
            @RequestParam(value = "page",defaultValue = "1")Integer pageNum,
            @RequestParam(value = "pagesize",defaultValue = "10")Integer pageSize,
            @RequestParam("uid")Long userId
    ){
        return userManager.findVideoByPage(userId,pageNum,pageSize);
    }

}
